<template>
   <div id="loginBox">
     <div class="loginMain">
        <div class="loginBox">
            <div class="logoBox">
              <img src="../assets/logo.png" class="logopc" />
            </div>
            <div class="loginContent">
              <div class="loginTitle">欢迎登录</div>
              <div class="loginInputbox">
                  <img src="../assets/user.png" class="loginIcon" />
                  <input type="text" v-model.trim="userName" class="loginInput" placeholder="用户名" />
              </div>
              <div class="loginInputbox">
                  <img src="../assets/password.png" class="loginIcon" />
                  <input type="password"  v-model.trim="userPassword" class="loginInput" placeholder="密码" />
              </div>
              <div class="remeberBox">
                 <el-checkbox v-model.trim="remeberText" @change="remeberPwd">记住密码</el-checkbox>
              </div>
              <div class="loginBtn" v-on:click="loginF">登录</div>
            </div>
        </div>
     </div>
   </div>
</template>

<script>
export default {
   name:'loginBox',
   data(){
     return {
        name:'123',
        userName:'',
        userPassword:'',
        remeberText:false
     }
   },
   destroyed:function(){//销毁
      window.removeEventListener('keydown',this.keyDown,false);
   },
    mounted:function(){
      //绑定事件
      window.addEventListener('keydown',this.keyDown);
      let userName = window.localStorage.getItem("userNameCookies")
      let userPassword = window.localStorage.getItem("userPasswordCookies")
      let remeber = window.localStorage.getItem("remeberCookies")

      
      if(userName == "null"){
        this.userName = ''
      }else{
        this.userName = userName
      }

      if(userPassword == "null"){
        this.userPassword = ''
      }else{
        this.userPassword = userPassword
      }
      
      this.remeberText = Boolean(remeber)
   },
   methods: {
      remeberPwd(){
         console.log(this.remeberText)
         if(this.remeberText){
            window.localStorage.setItem("userNameCookies",this.userName,'7d')
            window.localStorage.setItem("userPasswordCookies",this.userPassword,'7d')
            window.localStorage.setItem("remeberCookies",this.remeberText,'7d')
         }else{
            window.localStorage.setItem("userNameCookies",'','7d')
            window.localStorage.setItem("userPasswordCookies",'','7d')
            window.localStorage.setItem("remeberCookies",'','7d')
         }
      },
      // 键盘
      keyDown(e){
          //如果是回车则执行登录方法
        if(e.keyCode == 13){
          this.loginF();
        }
      },
      // 登录
      loginF(){
         let getuserName = this.userName;
         let getuserPassword = this.userPassword
         if(getuserName == '' || getuserName == null || getuserPassword == '' || getuserPassword == null ){
              this.$notify({
                title: '警告',
                message: '账号或密码不能为空！',
                type: 'warning'
              });
         }else{
             if(getuserName == 'mnn' & getuserPassword == '123' ){
                
                 this.$notify({
                    title: '提示',
                    message: '登录成功！',
                    type: 'success'
                  });
                  this.$router.push({path:'/Createmanage',query:{id:'mnn'}})
             }else{
                this.$notify({
                  title: '警告',
                  message: '账号或密码错误！',
                  type: 'warning'
                });
             }
         }
      }
   },
}
</script>

<style scoped>
  #loginBox{
    width: 100%;
    height: 100vh;
    position: relative;
  }
  .loginMain{
    width:100%;
    height: 100%;
    background: #f2f2f2;
  }
  .loginBox{
    position: absolute;
    padding:5vh 5vw;
    background: #fff;
    border-radius: 8px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
  .loginTitle{
    width: 100%;
    font-size: 20px;
    color: #333;
    font-weight: bold;
    margin-bottom: 25px;
  }
  .logoBox{
    width: 20vw;
    min-width: 300px;
    text-align: center;
    margin-bottom: 15px;
  }
  .logopc{
    width: 100px;
  }
  .loginInputbox{
    width: 20vw;
    min-width: 300px;
    height: 1.5rem;
    box-sizing: border-box;
    padding:5px;
    border:1px solid #ddd;
    border-radius: 5px;
    display: flex;
    height: 35px;
    align-items: center;
    margin-bottom: 20px;
  }
  .loginIcon{
    width:20px;
    height: 20px;
  }
  .loginInput{
    flex: 1;
    margin-left: 8px;
    font-size: 14px;
    color: #333;
    height: 100%;
    outline-style: none;
    border:0;
  }
  .loginBtn{
    width: 100%;
    height: 40px;
    background:#23bca1;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    cursor: pointer;
    color: #fff;
    border-radius: 8px;
    margin-top: 40px;
  }
  .remeberBox{
    width: 100%;
    overflow: hidden;
    text-align: left;
  }
</style>